home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / microcrn / issue_41.arc / KAYPRO41.ARC / COUNT.C next >
Encoding:
C/C++ Source or Header  |  1988-03-16  |  1.6 KB  |  73 lines

  1. /*  Code from Kaypro Column in Micro Cornucopia Magazine Issue #41
  2. *   COUNT --  Counts characters, words, and lines in input
  3. *
  4. *   Written for Small-C compiler vers. 2.03 (ASM)
  5. *
  6. */
  7.  
  8. #include <stdioa.h>
  9. #include "iolib.asm"
  10. #include "call.asm"
  11.  
  12. #define NOCCARGC
  13. #define HIGHBIT 127     /* high bit mask = 01111111b */
  14.  
  15. int infile;
  16. char fname[15];
  17.  
  18. main (argc, argv) int argc, argv[]; {
  19.  
  20.   char *lines, *words, *chars;  /* fake unsigned integers */
  21.   int inword, c;
  22.   fputs("COUNT vers. 8/24/87\n",stderr);
  23.   if (argc >= 2)
  24.       infile = fopen(argv[1],"r");
  25.   else {
  26.       fputs("Counts words, lines and chars in text file.\n",stderr);
  27.       fputs("Name of file to check: ? ",stderr);
  28.       gets(fname);
  29.       infile = fopen(fname,"r");
  30.    }
  31.  
  32.   if (infile == NULL) {
  33.       fputs("File not found.",stderr);
  34.       exit();
  35.    }
  36.  
  37.   fputs("Processing file...",stderr);
  38.   inword = NO;
  39.   lines = words = chars = 0;
  40.   while ((c = getc(infile)) != EOF) {
  41.     ++chars;
  42.     c &= HIGHBIT;        /* mask high bit for WordStar files */
  43.     if (c == '\n')
  44.         ++lines;
  45.     if (c==' ' || c=='\n' || c=='\t')
  46.         inword = NO;
  47.     else if (inword == NO) {
  48.       inword = YES;
  49.       ++words;
  50.     }
  51.   }
  52.   fputc('\n',stderr);
  53.   fputs("Chars   = ",stderr);
  54.   putnum(chars,stderr);
  55.   fputc('\n',stderr);
  56.   fputs("Words   = ",stderr);
  57.   putnum(words,stderr);
  58.   fputc('\n',stderr);
  59.   fputs("Lines   = ",stderr);
  60.   putnum(lines,stderr);
  61.   fputc('\n',stderr);
  62. }
  63.  
  64. /*
  65. *  write unsigned number n to fd as string.
  66. */
  67. putnum(n,fd) char *n; int fd; {
  68.   char numstr[7];
  69.   fputs(itou(n,numstr,6),fd);
  70. }
  71.  
  72. #include "itou.c"
  73.